import {getCreateGroupBuyInfoXCX,getBargainInfo1,getGoodsGroupBuyInfoXCX} from '../../../Tools/HTTPOpertion.js';
var app = getApp();
var appData = app.globalData;
Page({
  data: {
    userId: '',
    from: '',
    icon: 'https://mpic.denong.com/xcx-c-20180703-dui.png',
    isShow: 'none',
    alertTop: 0,
    gonlList: [],
    id: 0,
    obj: {},
    types:[],
    markers: [{
      iconPath: "https://activity.denong.com/dw.png",
        id: 0,
        latitude: 23.099994,
        longitude: 113.324520,
        width: 50,
        height: 50
    }],
    allLikes:[],
    likes:[],
    currentLikes: [],
    services:[],
    allServices:[],
    minServices: [],
    pics:[],
    hid: false,   //分享弹出框
    cnd: '',
    cnd1: '',
    cndOrderId:''
  },

  onLoad: function (options) {
      // 扫描二维码获取的数据（拼团页面为cnd）
      if (options.scene) {
        var scene = decodeURIComponent(options.scene)
        var arr = [];
        var obj = {};
        arr = scene.split('=');
        obj.cnd = arr[1];
        this.setData({
          from: obj.cnd,
          cnd: obj.cnd
        })
        // 将返回的cnd给页面数据
        this.loadData();
      }else{
          console.log(options);
          const userId = wx.getStorageSync('userId');
          this.setData({
            id: options.id,
            from: options.from||'',
            userId: userId,
          });
          this.loadData();
      }

  },
  showAlert:function(){
    this.setData({
      alertTop:50,
      isShow:"block"
    });
  },
  close:function(){
    this.setData({
      alertTop: 0,
      isShow: "none"
    });
  },
  ddhua:function(e){
    const phoneNumber = e.currentTarget.dataset.store_phone;
    wx.makePhoneCall({
        phoneNumber,
    })
  },
  shop_list_btn:function(e){
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: '/pages/others/coupon/coupon?id='+id
    })
  },
  tpTap: function (e) {
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/others/Album/Album?id=' + id
      })
  },

  callPhone:function(e){
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone
    })
  },
  oneKeyGroup: function (e){
    wx.navigateTo({
        url: '/pages/active/bargainConfirmOrder/bargainConfirmOrder?orderId=' + e.currentTarget.dataset.id
    })
  },
  toBarginOwn: function (e){
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url: `/pages/active/bargainOwnPage/bargainOwnPage?id=${id}`,
    })
  },
  toShop: function (e){
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url: `/pages/others/MerchantDetails/MerchantDetails?id=${id}`,
    })
  },
  onShareAppMessage: function () {
    console.log(1)
    const self = this;
    return {
      title: self.data.obj.name,
      path: `/pages/active/makeGroupsDetails/makeGroupsDetails?id=${self.data.obj.id}`,
      success: function(res) {
        // 转发成功
        wx.showToast({
          title: '转发成功',
          duration: 2000
        })

        self.setData({
          showModal: false,
          hid: false
        })
        console.log(self.data.hid)
      },
      fail: function(res) {
        // 转发失败
        wx.showToast({
          title: '转发失败',
          duration: 2000
        })
      }
    }
  },
  backHome: function (e) {
    wx.switchTab({
        url: '/pages/main/spellGroupHome/spellGroupHome',
    })
  },
  navToMap:function(e){
    console.log(e);
    var lat = e.currentTarget.dataset.info.latitude;
    var log = e.currentTarget.dataset.info.longitude;
    var name = e.currentTarget.dataset.info.name;
    wx.openLocation({
      latitude: parseFloat(lat),
      longitude: parseFloat(log),
      name: name,
      scale: 18
    })
  },
  showAll:function(){
    this.setData({
      currentLikes: this.data.currentLikes.length == this.data.likes.length ? this.data.allLikes : this.data.likes
    });
  },
  // 一键开团
  getCreateGroupBuyInfoXCX(e) {
    console.warn(e);
    const self = this;
    const group_buy_id = e.currentTarget.dataset.group_buy_id;
    const create_person_id = e.currentTarget.dataset.create_person_id;
    let config = {};
    if (create_person_id) {
      config = {
        cnd: self.data.id,
        group_buy_id,
        create_person_id,
      };
    } else {
      config = {
        cnd: self.data.id,
      };
    }
    getCreateGroupBuyInfoXCX(config).then(function (result) {
        wx.hideLoading();
        console.warn('result: ', result);
        if (result.code == 0) {
          wx.navigateTo({
            url: '/pages/others/ConfirmationOrder/ConfirmationOrder?cnd='+self.data.id+'&group_buy_id='+group_buy_id+'&create_person_id='+create_person_id,
          });
        }
    })
    .catch(function (error) {
      console.log(error);
    });
  },
  loadData: function () {
      var self = this;
      console.log(self.data.id);
      console.log('self.data',self.data);
      if (self.data.from == 'banner') {
        getBargainInfo1({ cnd: self.data.id }).then(function (result) {
            let explain_img_url = result.data.explain_img_url;
            if (explain_img_url) {
              explain_img_url = JSON.parse(explain_img_url);
            } else {
              explain_img_url = "";
            }
            console.warn('create_person_id：',result.data.create_person_id,'userId: ', self.data.userId, 'obj: ', result.data);
            self.setData({
                obj: result.data,
                'obj.imgList': explain_img_url,
                'obj.xiaojin': result.data.currency/100,
                'cndOrderId': result.data.cndOrderId
            });
            console.warn(self.data.obj);
        })
            .catch(function (error) {
                console.log(error);
            });
      } else {
        getGoodsGroupBuyInfoXCX({ cnd: self.data.id }).then(function (result) {
            wx.hideLoading();
            let explain_img_url = result.data.explain_img_url;
            if (explain_img_url) {
              explain_img_url = JSON.parse(explain_img_url);
            } else {
              explain_img_url = "";
            }
            console.warn('create_person_id：',result.data.create_person_id,'userId: ', self.data.userId, 'obj: ', result.data);
            self.setData({
                obj: result.data,
                'obj.imgList': explain_img_url,
                'obj.xiaojin': result.data.currency/100,
                'cndOrderId': result.data.cndOrderId
            });
            console.log(self.data.obj);
        })
            .catch(function (error) {
                console.log(error);
            });
      }
    },
    showAll:function(){
      this.setData({
        services: this.data.services.length > 8 ? this.data.minServices : this.data.allServices
      });
      console.log("showAll...", this.data.services);
    },
    toNextPage: function (e) {
      console.log(e);
      var pageIndex = e.currentTarget.dataset.page;
      var pages = ["/pages/others/homePage/homePage", "/pages/others/FightGroups/FightGroups", "/pages/others/search/search", '/pages/others/goodDetail/goodDetail']
      wx.navigateTo({
        url: pages[pageIndex],
      })
    }
})
